package com.renwei.yunlong.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.adapter.SelectSponsorAdapter;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.ServiceLoginBean;
import com.renwei.yunlong.bean.SponsorBean;
import com.renwei.yunlong.http.HttpPresenter;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.ACache;
import com.zyf.fwms.commonlibrary.http.Api;
import com.zyf.fwms.commonlibrary.utils.AutoUtils;
import com.zyf.fwms.commonlibrary.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 选择发起人--页面
 * <p>
 * Created by Administrator on 2017/8/31.
 */

public class SelectSponsorActivity extends BaseActivity implements View.OnClickListener, HttpTaskListener {

    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.rl_search)
    RelativeLayout rlSearch;
    @Bind(R.id.et_search)
    EditText etSearch;

    private ACache mCache;
    private SelectSponsorAdapter adapter;

    String bindFlag;
    String companyCode;
    ServiceLoginBean bean;
    String content;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_select_sponsor);
        ButterKnife.bind(this);

        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);
        mCache = ACache.get(this);
        //屏幕自适配
        AutoUtils.setSize(this, false, 720, 1280);
        AutoUtils.auto(this);

        initData();
    }

    private void initData() {
        ivBack.setVisibility(View.VISIBLE);
        ivBack.setOnClickListener(this);
        tvTitle.setText("选择发起人");
        rlSearch.setOnClickListener(this);

        bindFlag = getIntent().getStringExtra("bindFlag");
        companyCode = getIntent().getStringExtra("companyCode");
        bean = (ServiceLoginBean) mCache.getAsObject("serviceLoginBean");
        content = etSearch.getText().toString().trim();
        if (!TextUtils.isEmpty(bindFlag) && !TextUtils.isEmpty(bindFlag) && bean != null) {
            if (content != null){
                String jsonBean = "{bindFlag:\"" + bindFlag + "\",synCode:\"" + companyCode + "\",serviceProviderCode:\"" + bean.getRows().getServiceProviderCode() + "\",name:\"" + "" + "\"}";
                serviceSelectSponsorHttp(jsonBean);
                Log.e("业主jsonBean",jsonBean);
            }
        }
    }

    //服务商发起人
    private void serviceSelectSponsorHttp(String jsonBean) {
        HttpPresenter hp = new HttpPresenter(SelectSponsorActivity.this);
        hp.setUrl(Api.SERVICE_QUERYEMPLOYEE_URL);
        hp.setshowDialogcontent("正在加载");
        hp.setRequsetId(1);
        hp.putParam("jsonBean", jsonBean);
        hp.setCallBack(this);
        hp.post();
    }

    private void setListDate(List<SponsorBean.Rows> mList) {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new SelectSponsorAdapter(this, mList);
        adapter.setOnItemClickListener(new SelectSponsorAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, String name,String employeeId) {
                mCache.put("employeeId",employeeId);
                Intent intent = new Intent();
                intent.putExtra("name",name);
                setResult(RESULT_OK, intent);
                SelectSponsorActivity.this.finish();
            }
        });
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.rl_search:
                //{"bindFlag":是否绑定1是，2否， “synCode”：选择发起业主公司code，"serviceProviderCode":当前公司Code,按名称查询, “name”：名称}
                if (!TextUtils.isEmpty(bindFlag) && !TextUtils.isEmpty(bindFlag) && bean != null) {
                    if (content != null){
                        String jsonBean = "{bindFlag:\"" + bindFlag + "\",synCode:\"" + companyCode + "\",serviceProviderCode:\"" + bean.getRows().getServiceProviderCode() + "\",name:\"" + content + "\"}";
                        serviceSelectSponsorHttp(jsonBean);
                        Log.e("业主jsonBean",jsonBean);
                    }
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onSuccess(int requestId, String result) {
        switch (requestId) {
            case 1:
                Log.e("业主返回数据",result);
                SponsorBean sponsorBean = new Gson().fromJson(result,SponsorBean.class);
                if (sponsorBean.getMessage().getCode() == 200){
                    setListDate(sponsorBean.getRows());
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onException(int requestId, String code) {

    }
}
